package com.jumi.microservice.warehousein.controller;

import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.exception.ExceptionEnum;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.validator.group.InsertGroup;
import com.jumi.microservice.common.validator.group.QueryGroup;
import com.jumi.microservice.common.validator.group.UpdateGroup;
import com.jumi.microservice.warehousein.domain.JmWarehouseIn;
import com.jumi.microservice.warehousein.domain.JmWarehouseInAuditLog;
import com.jumi.microservice.warehousein.dto.JmWarehouseInAuditRequest;
import com.jumi.microservice.warehousein.dto.JmWarehouseInRequest;
import com.jumi.microservice.warehousein.dto.JmWarehouseInResponse;
import com.jumi.microservice.warehousein.service.JmWarehouseInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author ssk
 * @create 2020/8/11 15:20
 **/
@RestController
@RequestMapping("/warehouseIn")
@Validated
@Api(tags = {"【入库管理】入库管理接口"})
public class JmWarehouseInController extends BaseController {

    @Resource
    private JmWarehouseInService inService;


    @PostMapping("/add")
    @ApiOperation("添加入库申请接口")
    @Log(title = "供应链管理-入库管理-入库申请",businessType = BusinessType.INSERT)
    public ResponseResult add(@Validated({InsertGroup.class}) @RequestBody JmWarehouseInRequest inRequest,@RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        JmWarehouseIn warehouseIn = new JmWarehouseIn();
        try {
            inRequest.clone(warehouseIn);
        } catch (Exception e) {
            throw ExceptionEnum.FAIL_CHANGE.newException();
        }
        warehouseIn.setCreateBy(createBy);
        int rows = inService.save(warehouseIn);
        return ResponseResult.success("操作成功", warehouseIn.getInId());
    }

    @PostMapping("/edit")
    @ApiOperation("编辑入库申请接口")
    @Log(title = "供应链管理-入库管理-入库申请",businessType = BusinessType.UPDATE)
    public ResponseResult edit(@Validated({UpdateGroup.class}) @RequestBody JmWarehouseInRequest inRequest,@RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmWarehouseIn warehouseIn = new JmWarehouseIn();
        try {
            inRequest.clone(warehouseIn);
        } catch (Exception e) {
            throw ExceptionEnum.FAIL_CHANGE.newException();
        }
        warehouseIn.setUpdateBy(updateBy);
        int rows = inService.update(warehouseIn);
        return ResponseResult.success("操作成功", warehouseIn.getInId());
    }


    @GetMapping("/findById")
    @ApiOperation("根据ID查询入库申请接口")
    @ApiImplicitParam(name = "inId",value = "入库申请ID")
    public ResponseResult<JmWarehouseInResponse> findById(@NotNull(message = "ID不能为空")Long inId) {
        JmWarehouseInResponse response = inService.findById(inId);
        return ResponseResult.success(response);
    }


    @GetMapping("/findByPage")
    @ApiOperation("分页查询接口")
    public TableDataInfo<JmWarehouseInResponse> findByPage(@Validated({QueryGroup.class}) JmWarehouseInRequest inRequest) {
        startPage();
        List<JmWarehouseInResponse> list = inService.findAll(inRequest);
        return getDataTable(list);
    }

    @PostMapping("/auditBatch")
    @ApiOperation("批量审核入库申请接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inIds",value = "需要审核的ID集合"),
            @ApiImplicitParam(name = "auditRemark",value = "备注"),
            @ApiImplicitParam(name = "price",value = "成本价"),
            @ApiImplicitParam(name = "status",value = "审核状态;0=待审核,1=已审核,2=已入库,3=未通过")
    })
    @Log(title = "供应链管理-入库管理-入库申请",businessType = BusinessType.AUDIT)
    public ResponseResult auditBatch(@NotNull(message = "缺失审核入库申请ID") Long[] inIds, String auditRemark,
                                     BigDecimal price, @NotNull(message = "审核状态不能为空") Integer status,@RequestHeader(RequestConstant.ADMIN_NAME) String auditBy) {
        int rows = inService.auditBatch(inIds, auditRemark, price, status, auditBy);
        return ResponseResult.success("操作成功", rows);
    }
    @PostMapping("/audit")
    @ApiOperation("审核入库申请接口")
    @Log(title = "供应链管理-入库管理-入库申请",businessType = BusinessType.AUDIT)
    public ResponseResult audit(@RequestBody JmWarehouseInAuditRequest auditRequest,@RequestHeader(RequestConstant.ADMIN_NAME) String auditBy){
        auditRequest.setAuditBy(auditBy);
        String name = getUserName();
        int rows=inService.audit(auditRequest);
        return ResponseResult.success("操作成功",auditRequest.getInId());
    }

    @GetMapping("/findAuditLogById")
    @ApiOperation("根据ID查询审核日志接口")
    @ApiImplicitParam(name = "inId",value = "入库申请ID")
    public TableDataInfo<JmWarehouseInAuditLog> findAuditLogById(@NotNull(message = "ID不能为空") Long inId) {
        startPage();
        List<JmWarehouseInAuditLog> logs = inService.findAuditLogById(inId);
        return getDataTable(logs);
    }

    @PostMapping("/syncPurchaseOrders")
    @ApiOperation("根据旺店通采购单号确认入库")
    public ResponseResult SyncPurchaseOrders(@RequestHeader(RequestConstant.ADMIN_NAME) String auditBy){
        int rows=inService.syncPurchaseOrders(auditBy);
        return ResponseResult.success("操作成功",rows);
    }


}
